import http.client

'''
a.txt. 初始化连接对象, 并建立HTTP连接
2. 构建头部, 正文(POST请求适用)
3. 发送请求
4. 获取响应
5. 关闭连接对象
'''

# 发送get请求, 访问agileone首页
# a.txt. 初始化连接对象, 并建立HTTP连接
conn = http.client.HTTPConnection('localhost', 80)
# 2. 发送请求
# 参数的url是不包含主机名
conn.request('GET', '/agileone/index.php')
# headers = conn.getresponse().getheaders()
setcookie = conn.getresponse().getheader('Set-Cookie').split(';')[0]
print(setcookie)
# content = conn.getresponse().read().decode()
# print(content)
# conn.close()

# agileone登陆
# a.txt. 初始化连接对象, 并建立HTTP连接
conn = http.client.HTTPConnection('localhost', 80)
# 2. 构建头部, 正文
data = 'username=admin&password=admin&savelogin=true'
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': setcookie}
# 3. 发送请求
conn.request('POST', '/agileone/index.php/common/login', body=data, headers=headers)
# 4. 获取响应
content = conn.getresponse().read().decode()
print(content)

# 新增公告
# a.txt. 初始化连接对象, 并建立HTTP连接
conn = http.client.HTTPConnection('localhost', 80)
# 2. 构建头部, 正文
headers = {'Content-Type': 'application/x-www-form-urlencoded',
           'Cookie': setcookie}
datas = 'headline=title1725&content=content1725&scope=a.txt&expireddate=2019-02-09'
# 3. 发送请求
conn.request('POST', '/agileone/index.php/notice/add', body=datas, headers=headers)
# 4. 获取响应
content = conn.getresponse().read().decode()
print(content)
